import Databus from './databus'
import ResourceLoader  from './base/resourceLoader'
import Background from './runtime/background'
import Land from './runtime/land'
import Pipe from './runtime/pipe'
import Bird from './players/bird'
import Score from './players/score'
import Over from './players/over'
import Pad from './players/pad'
const databus = new Databus()
// x修改修改渲染帧率
wx.setPreferredFramesPerSecond(40)
export default class Main {
    constructor(){
        // 获取画布及绘图上下文 设置为自己的属性
        this.canvas = wx.createCanvas()
        this.ctx = this.canvas.getContext('2d')
        this.aniId = 0
        // 存储公用变量 画布和画布的上下文
        databus.canvas = this.canvas
        databus.ctx = this.ctx
        this.resource = new ResourceLoader()
        this.status = false
        this.loop()
        this.bindEvent()
    }
    // 初始化方法
    init(){
        if(this.status) return
        this.status = true
        // 清空数据
        databus.reset()
        this.bg = new Background()
        this.land = new Land()
        this.bird = new Bird()
        this.score = new Score()
        this.over = null
        this.bg.render()
        this.land.render()
    }
    update(){
        // 重新绘制 清空画布--重新调用background.update()-再重新渲染
        databus.ctx.clearRect(0,0,databus.canvas.width,databus.canvas.height)
        // this.bg.update()
        // this.bg.render()
        // this.land.update()
        // this.land.render()
        databus.actors.forEach(value => {
            value.update()
            value.render()
        })
        this.score.render()
    }

    loop () {
        // 开启定时器
       var number = requestAnimationFrame(() => {
            // 参数是一个回调函数 判断图片是否加载完成
            // console.log(number)
            // console.log(this.aniId)
            this.aniId += number
            // console.log(this.aniId)
            if(databus.load){
                // console.log(this.aniId)
                // 场景判断
                if (databus.scene === 0) {
                    // 初始化
                    this.init()
                    // console.log(this.aniId)
                    // 每100帧添加一对管子
                    if (this.aniId % 100 === 0){
                        const pipe = new Pipe()
                    }
                } else if (databus.scene === 2) {
                    databus.speed = 0
                    databus.bird.wing = 0
                    databus.bird.rotate = Math.PI / 2
                    if (!this.over){
                        this.over = new Over()
                        this.pad = new Pad()
                    }
                    if (databus.score > databus.max) {
                        databus.max = databus.score
                    }
                }
                // 调用方法
                this.update()
            }
            this.loop()
        })
    }
    bindEvent(){
        wx.onTouchStart((result) => {
            if (databus.scene === 0) {
                this.bird.bindFly()
                databus.scene === 1
            } else if(databus.scene === 1) {
                this.bird.bindFly()
            } else if (databus.scene === 2) {
                databus.scene = 0
                this.status = false
            }
        })
    }
}